<?php

namespace App\Daos\Admin\Users;

use App\Daos\Admin\DbDao;
use App\Models\Coupons\Coupons;
use App\Models\Users\Genre;
use App\Models\Users\Grade;
use App\Models\Users\Group;
use App\Models\Coupons\Logs;
use App\Models\Users\Source;
use App\Models\Users\Tag;
use App\Models\Users\Users;

class UsersDao
{
    private static $client;
    public static function userSelect ($search_part,$search_other,$search_tag,$search_visit,$search_order_num,$limit){
        self::$client = DbDao::connent();
        $query_master = "select shop_users.`id`,uid,shop_users.`icon`,shop_users.`name`,vip,shop_grades.`grade_name` as 'grade',shop_user_group.`name` as 'group',tel,shop_user_source.`name` as 'type',balance,tag FROM shop_users join shop_grades on shop_users.grade=shop_grades.id join shop_user_group on shop_users.group=shop_user_group.id join shop_user_source on shop_users.type=shop_user_source.id";
        $query_slave = '';
        if (!empty($search_part['value'])){
            if (empty($search_part['key'])){
                $query_slave .= "CONCAT(uid,tel,shop_users.`name`) like '%" . $search_part['value'] . "%' and ";
            } else {
                $query_slave .= "shop_users." . $search_part['key'] . " like '%" . $search_part['value'] . "%' and ";
            }
        }
        if (!empty($search_visit['state'])){
            switch ($search_visit['state']){
                case 1:
                    $query_slave .= "shop_users.last_visit between '" . $search_visit['date'][0] . "' and '" . $search_visit['date'][1] . "' and ";
                    break;
                case 2:
                    $query_slave .= "shop_users.last_visit > '" . $search_visit['date'][1] . "' and last_visit < '" . $search_visit['date'][0] . "' and ";
                    break;
                case 3:
                    $query_slave .= "shop_users.create_time between '" . $search_visit['date'][0] . "' and '" . $search_visit['date'][1] . "' and ";
                    break;
            }
        }
        if (!empty($search_order_num)){
            if ($search_order_num == 0){
                $query_slave .= "shop_users.order_num = 0 and ";
            } else {
                $query_slave .= "shop_users.order_num >= " . $search_order_num . " and ";
            }
        }
        if (!empty($search_tag)){
            foreach ($search_tag as $value){
                $query_slave .= "FIND_IN_SET('$value',shop_users.`tag`) or ";
            }

        }
        foreach ($search_other as $key=>$value){
            if (!empty($value)){
                $query_slave .= "shop_users.`" . $key . "` = '" . $value . "' and ";
            }
        }
        $state = true;
        if (empty($query_slave)){
            $query = $query_master;
            $query_count = "select count(1) as 'count' from shop_users";
        } else {
            $query = $query_master . " where " . $query_slave;
            $query_count = "select count(1) as 'count' from shop_users" . " where " . $query_slave;
            $query = substr($query,0,-4);
            $query_count = substr($query_count,0,-4);
            $state = false;
        }
        $query .= " limit " .  $limit['offset'] . " , " . $limit['size'];
        $master = self::$client->prepare($query);
        $count = self::$client->prepare($query_count);
        $master->execute();
        $count->execute();
        $data['users'] = $master->fetchAll();
        $data['count'] = $count->fetchAll();
        if ($state){
            $data['tags'] = Tag::get(['id','name'])->toArray();
            $data['grades'] = Grade::get(['id','grade_name'])->toArray();
            $data['groups'] = Group::get(['id','name'])->toArray();
            $data['source'] = Source::get(['id','name'])->toArray();
            $data['class'] = Genre::get(['id','name'])->toArray();
        }
        return $data;
    }

    public static function userAdd ($data){
        $uid = substr(strtotime(date('Y-m-d H:i:s')),-3) . rand(10,99);
        $res = Users::create([
            'uid' => $uid,
            'name' => $data['name'],
            'tel' => $data['tel'],
            'birth' => $data['birth'],
            'idcard' => $data['idcard'],
            'addr' => $data['addr'],
            'remark' => $data['remark'],
            'password' => md5($data['password'] . 'rfzs'),
            'grade' => $data['grade'],
            'group' => $data['group'],
            'tag' => implode(',',$data['tag']),
            'expand_status' => $data['status'],
            'expand_power' => $data['power'],
            'state' => $data['state'],
            'created_at' => date('Y-m-d H:i:s')
        ]);
        if (!$res){
            throw new \Exception('添加失败');
        }
        return $res;
    }

    public static function couponList ($offset,$search){
        $data['data'] = Coupons::where('coupons_way','赠送券')->when($search, function ($query,$search){
            $query->where('coupons_name', 'like', "$search%");
        })->offset($offset)->limit(3)->get();
        $data['total'] = Coupons::where('coupons_way','赠送券')->when($search, function ($query,$search){
            $query->where('coupons_name', 'like', "$search%");
        })->get()->count();
        return $data;
    }

    public static function sendCoupon ($user,$coupon){
        if ($coupon['is_use_time'] == 0){
            $coupon['use_time_num'] = $coupon['use_time_num'] * 24;
        }
        $user = Users::find($user);
        Logs::create([
            'uid' => $user['id'],
            'poupons_id' => $coupon['id'],
            'user_name' => $user['name'],
            'coupons_name' => $coupon['coupons_name'],
            'coupons_price' => $coupon['coupons_price'],
            'minimun_use_price' => $coupon['coupons_threshold'],
            'start_time' => date('Y-m-d H:i:s'),
            'stop_time' => date('Y-m-d H:i:s', strtotime("+ " . $coupon['use_time_num'] . " hour")),
            'get_way' => 3,
            'is_start' => 1,
            'start' => 0,
            'created_at' => date('Y-m-d H:i:s')
        ]);
        return true;
    }

    public static function sendVip ($id,$num){
        $user = Users::find($id);
        if ($user->vip == 1){
            $user->member_time = date('Y-m-d H:i:s', strtotime("$user->member_time + $num day"));
        } else {
            $user->vip = 1;
            $user->member_time = date('Y-m-d H:i:s', strtotime("+ $num day"));
        }
        $user->save();
        if (!$user){
            return false;
        }
        return true;
    }
}
